#define _MSOVD_C_
#include <math.h>
#include "types.h"
#include "board.h"
#include "ms_rwreg.h"
#include "ms_reg.h"
#include "msfb.h"
#include "mcu.h"
#include "mode.h"
#include "UserPrefDef.h"
#include "global.h"

//Internal
#include "msOVD.h"

typedef enum _ChipType
{
    CHIP_WARHOL,
    CHIP_RIBERA,
} ChipType;

typedef enum _ODRAMType
{
    OD_RAM_1M,//OD_MODE444_COMP
    OD_RAM_2M,//OD_MODE565
} ODRAMType;

ChipType  xdata msChipType = CHIP_WARHOL;
ODRAMType xdata msRAMType = OD_RAM_1M;

#define BIT0    0x0001
#define BIT1    0x0002
#define BIT2    0x0004
#define BIT3    0x0008
#define BIT4    0x0010
#define BIT5    0x0020
#define BIT6    0x0040
#define BIT7    0x0080


//#define OD_LIB_VER        0x03
//code ==================================================================

//*************************************************************************************************
//Function:  msODMemoryTest()
//Description: For Memory test
//
//           ex:    printData(" msODMemoryTest = %d"  , msODMemoryTest());
//           if the result is 0,
//           if the result is 1,
//
//*************************************************************************************************

//*************************************************************************************************
//Function:  msOD_Reset()
//Description: Reset MIU bank
//caller:        Power_TurnOnPanel of power.c
//
//           It need to reset MIU bank before turn on panel.
//*************************************************************************************************


//*************************************************************************************************
//Function:  msOD_OnOff()
//Description: Enable/Disable OverDrive and setup OD strength
//          ex:
//          msOD_OnOff( _OD_Disable, 0x50 ); //disable OD
//          msOD_OnOff( _OD_Enable, 0x5F ); //enable OD and set strength as 0x5F
//          OD strength: 0x50 (weak)  <------------>  0x5F (strong)
//
//*************************************************************************************************
//*************************************************************************************************
//Function:  msLoadODT()
//Description: OD look-up table loader.
//
//           ex: msLoadODT(tOverDrive)
//
//           It provides two kinds of format (normal and compressed) for OD look-up table.
//           The look-up table and OD lib should be located in the same bank!!
//
//*************************************************************************************************
#if ENABLE_RTE
BYTE code tOverDrive[] =
{
    0xD9, 0xC2, 0xEA, 0x97, 0xB1, 0x5A, 0x47, 0x6F,
    0x10, 0xD9, 0x3D, 0x37, 0x2E, 0x27, 0x26, 0x26,
    0xD9, 0xC9, 0xF3, 0xE4, 0x88, 0xB2, 0x5B, 0x40,
    0x69, 0x1C, 0x0D, 0x06, 0x34, 0x21, 0x26, 0x26,
    0xD9, 0xDF, 0xF9, 0xE3, 0x95, 0xBD, 0xA2, 0x4A,
    0x70, 0x67, 0x17, 0x02, 0x31, 0x2D, 0x26, 0x26,
    0xD9, 0xDC, 0xCC, 0xE9, 0x90, 0x87, 0xAD, 0x55,
    0x7A, 0x60, 0x12, 0x01, 0x3C, 0x2A, 0x26, 0x26,
    0xD9, 0xDC, 0xCA, 0xFA, 0x99, 0x82, 0xB6, 0x5C,
    0x45, 0x6A, 0x1D, 0x0D, 0x3A, 0x28, 0x26, 0x26,
    0xD9, 0xDA, 0xC8, 0xFA, 0xEF, 0x89, 0xB5, 0x58,
    0x4E, 0x77, 0x1A, 0x08, 0x39, 0x36, 0x26, 0x26,
    0xD9, 0xDA, 0xC9, 0xF8, 0xEA, 0x9F, 0xB9, 0xA5,
    0x4B, 0x70, 0x67, 0x16, 0x05, 0x35, 0x26, 0x26,
    0xD9, 0xDA, 0xD7, 0xF9, 0xEB, 0x9B, 0x8B, 0xA9,
    0x54, 0x79, 0x60, 0x13, 0x03, 0x33, 0x26, 0x26,
    0xD9, 0xDA, 0xD4, 0xC6, 0xF6, 0x98, 0x88, 0xBD,
    0x59, 0x43, 0x76, 0x1A, 0x00, 0x30, 0x26, 0x26,
    0xD9, 0xDB, 0xD5, 0xC3, 0xF1, 0xE1, 0x93, 0xB9,
    0xAF, 0x49, 0x7C, 0x62, 0x16, 0x3E, 0x27, 0x26,
    0xD9, 0xDB, 0xD3, 0xC1, 0xF1, 0xE1, 0x91, 0x81,
    0xB5, 0x5F, 0x79, 0x6E, 0x12, 0x3C, 0x27, 0x26,
    0xD9, 0xD8, 0xD0, 0xCF, 0xFD, 0xE9, 0xE6, 0x8A,
    0xBF, 0xA4, 0x4F, 0x69, 0x16, 0x39, 0x25, 0x26,
    0xD9, 0xD8, 0xD1, 0xCA, 0xC4, 0xF4, 0xE5, 0x95,
    0x84, 0xA9, 0x54, 0x7F, 0x19, 0x03, 0x20, 0x26,
    0xD9, 0xD9, 0xDC, 0xC9, 0xC1, 0xFA, 0xEA, 0x9A,
    0x8B, 0xBC, 0xA0, 0x44, 0x6D, 0x09, 0x37, 0x25,
    0xD9, 0xD9, 0xDD, 0xD0, 0xC9, 0xC2, 0xFC, 0xEA,
    0xE4, 0x94, 0x84, 0xA8, 0x73, 0x1F, 0x39, 0x2E,
    0xD9, 0xD9, 0xD8, 0xDF, 0xD0, 0xD6, 0xC1, 0xF8,
    0xF2, 0xEE, 0x9C, 0x8C, 0xB0, 0x64, 0x0F, 0x29,
    0xD9, 0xD9, 0xD9, 0xD9, 0xDB, 0xDC, 0xD0, 0xC9,
    0xCC, 0xFA, 0xEB, 0x9B, 0x8F, 0xA9, 0x14, 0x3D,
    0x4A, 0x61, 0x79, 0x1C, 0x31, 0xD6, 0xED, 0xF8,
    0x96, 0x98, 0xAE, 0xB5, 0xBD, 0xB8, 0xB8, 0xB8,
    0x4F, 0x5A, 0x74, 0x47, 0x19, 0x30, 0xC9, 0xE2,
    0xFC, 0x8B, 0x9D, 0xA1, 0xB5, 0xBA, 0xB8, 0xB8,
    0x44, 0x54, 0x6A, 0x04, 0x1E, 0x28, 0xC0, 0xDA,
    0xF4, 0x81, 0x92, 0xA6, 0xAA, 0xBC, 0xB8, 0xB8,
    0x45, 0x4A, 0x64, 0x7B, 0x14, 0x2F, 0x39, 0xD0,
    0xEA, 0x85, 0x96, 0x99, 0xAB, 0xBE, 0xB8, 0xB8,
    0x45, 0x4C, 0x5C, 0x75, 0x0B, 0x23, 0x3E, 0xD5,
    0xE0, 0xFC, 0x8B, 0x9C, 0xAE, 0xBF, 0xB8, 0xB8,
    0x45, 0x4D, 0x5D, 0x6A, 0x04, 0x19, 0x30, 0xCB,
    0xE5, 0xFF, 0x8E, 0x9F, 0xA0, 0xB0, 0xB8, 0xB8,
    0x45, 0x4E, 0x5F, 0x6D, 0x7A, 0x14, 0x29, 0xC1,
    0xDA, 0xF4, 0x81, 0x91, 0xA3, 0xB1, 0xB8, 0xB8,
    0x45, 0x4E, 0x51, 0x6E, 0x7D, 0x0D, 0x26, 0x39,
    0xD0, 0xEA, 0x86, 0x95, 0xA5, 0xB3, 0xB8, 0xB8,
    0x45, 0x4F, 0x53, 0x61, 0x70, 0x0E, 0x1F, 0x34,
    0xC8, 0xE3, 0xFE, 0x8A, 0xA6, 0xB4, 0xB8, 0xB8,
    0x46, 0x41, 0x55, 0x66, 0x68, 0x05, 0x12, 0x2D,
    0xC4, 0xDD, 0xF7, 0x82, 0x99, 0xB5, 0xB8, 0xB8,
    0x46, 0x41, 0x56, 0x67, 0x68, 0x07, 0x17, 0x26,
    0x3D, 0xD4, 0xEB, 0x86, 0x92, 0xB6, 0xB8, 0xB8,
    0x46, 0x42, 0x57, 0x59, 0x6E, 0x70, 0x0F, 0x1B,
    0x37, 0xCD, 0xE4, 0xF8, 0x9F, 0xA9, 0xB8, 0xB8,
    0x46, 0x43, 0x4A, 0x5F, 0x61, 0x73, 0x03, 0x13,
    0x20, 0x3C, 0xDD, 0xF4, 0x97, 0xAC, 0xB8, 0xB8,
    0x47, 0x45, 0x4C, 0x53, 0x59, 0x6C, 0x7C, 0x0C,
    0x1C, 0x28, 0xC3, 0xED, 0x84, 0x99, 0xBC, 0xB8,
    0x47, 0x46, 0x40, 0x4B, 0x51, 0x58, 0x6C, 0x70,
    0x02, 0x12, 0x21, 0x3C, 0xFD, 0x94, 0xB7, 0xBA,
    0x47, 0x47, 0x44, 0x40, 0x4C, 0x53, 0x5B, 0x62,
    0x77, 0x7A, 0x0A, 0x18, 0x3F, 0x8D, 0xA4, 0xBF,
    0x47, 0x47, 0x47, 0x46, 0x43, 0x40, 0x4B, 0x55,
    0x5C, 0x6D, 0x7D, 0x0C, 0x24, 0x38, 0x9D, 0xB4,
    0xF6, 0xE3, 0xD8, 0xB3, 0xAA, 0x80, 0x66, 0x51,
    0x4A, 0x39, 0x2A, 0x10, 0x04, 0x0D, 0x09, 0x09,
    0xF6, 0xFD, 0xD3, 0xCD, 0xB8, 0x91, 0x88, 0x60,
    0x5A, 0x37, 0x27, 0x2B, 0x1C, 0xF6, 0x09, 0x09,
    0xF6, 0xF3, 0xEC, 0xC2, 0xBF, 0x96, 0x81, 0x78,
    0x53, 0x4D, 0x3A, 0x2F, 0x10, 0x05, 0x09, 0x09,
    0xF6, 0xF3, 0xE2, 0xDF, 0xB2, 0xAA, 0x87, 0x71,
    0x69, 0x40, 0x31, 0x20, 0x12, 0x04, 0x09, 0x09,
    0xF6, 0xF2, 0xE4, 0xD5, 0xCD, 0xA2, 0x9B, 0x74,
    0x6F, 0x46, 0x35, 0x24, 0x17, 0x06, 0x09, 0x09,
    0xF6, 0xF5, 0xE6, 0xD4, 0xC2, 0xBD, 0x90, 0x89,
    0x63, 0x5D, 0x36, 0x26, 0x28, 0x1B, 0x09, 0x09,
    0xF6, 0xF5, 0xF9, 0xD6, 0xC4, 0xB2, 0xAF, 0x80,
    0x66, 0x52, 0x4D, 0x3A, 0x2D, 0x1D, 0x09, 0x09,
    0xF6, 0xF5, 0xFB, 0xE9, 0xD9, 0xB7, 0xA7, 0x9C,
    0x70, 0x6B, 0x42, 0x30, 0x2F, 0x1F, 0x09, 0x09,
    0xF6, 0xF4, 0xFA, 0xEA, 0xDD, 0xCA, 0xBB, 0x94,
    0x8D, 0x60, 0x5C, 0x49, 0x22, 0x1E, 0x08, 0x09,
    0xF6, 0xF4, 0xFD, 0xEF, 0xDE, 0xCE, 0xBC, 0xAA,
    0x86, 0x7D, 0x54, 0x4F, 0x3B, 0x10, 0x08, 0x09,
    0xF6, 0xF7, 0xFF, 0xE1, 0xD0, 0xC2, 0xB5, 0xA3,
    0x9F, 0x76, 0x6D, 0x42, 0x3B, 0x14, 0x0B, 0x09,
    0xF6, 0xF7, 0xFE, 0xE2, 0xD6, 0xD8, 0xCB, 0xB9,
    0x97, 0x80, 0x66, 0x5D, 0x30, 0x2B, 0x0C, 0x09,
    0xF6, 0xF6, 0xF0, 0xE7, 0xEC, 0xDE, 0xC1, 0xB1,
    0xA1, 0x9C, 0x75, 0x54, 0x4C, 0x23, 0x05, 0x0B,
    0xF6, 0xF6, 0xF2, 0xFA, 0xE2, 0xE9, 0xDA, 0xCD,
    0xB1, 0xAF, 0x9D, 0x71, 0x59, 0x3D, 0x11, 0x0F,
    0xF6, 0xF6, 0xF4, 0xF1, 0xFA, 0xE3, 0xE8, 0xDC,
    0xC2, 0xB4, 0xA7, 0x95, 0x7F, 0x34, 0x2D, 0x05,
    0xF6, 0xF6, 0xF6, 0xF5, 0xF3, 0xFC, 0xE6, 0xEE,
    0xD6, 0xDB, 0xCD, 0xBD, 0xA9, 0x60, 0x24, 0x1C,
    0x92, 0xB8, 0xA2, 0xCB, 0xF0, 0x1E, 0x06, 0x28,
    0x5C, 0x4F, 0x7B, 0x76, 0x6C, 0x64, 0x65, 0x65,
    0x9F, 0x82, 0xAA, 0xDF, 0xC1, 0xE8, 0x10, 0x3B,
    0x2D, 0x53, 0x4D, 0x79, 0x75, 0x66, 0x65, 0x65,
    0x98, 0x8A, 0xB2, 0xA4, 0xCC, 0xF1, 0x18, 0x9A,
    0x2A, 0x5E, 0x49, 0x45, 0x76, 0x60, 0x65, 0x65,
    0x98, 0x97, 0x85, 0xA2, 0xCA, 0xFC, 0xE1, 0x0E,
    0x30, 0x24, 0x54, 0x46, 0x70, 0x62, 0x65, 0x65,
    0x98, 0x90, 0x80, 0xB4, 0xD2, 0xFB, 0xE2, 0x15,
    0x3E, 0x23, 0x50, 0x43, 0x72, 0x6D, 0x65, 0x65,
    0x98, 0x93, 0x83, 0xB1, 0xDA, 0xC2, 0xE9, 0x13,
    0x05, 0x2F, 0x5D, 0x4D, 0x7F, 0x6C, 0x65, 0x65,
    0x98, 0x93, 0x8D, 0xB3, 0xA1, 0xD4, 0xF2, 0x18,
    0x00, 0x2A, 0x59, 0x4E, 0x79, 0x6F, 0x65, 0x65,
    0x98, 0x92, 0x8E, 0xB2, 0xAD, 0xD3, 0xC6, 0xE2,
    0x09, 0x32, 0x27, 0x55, 0x7B, 0x69, 0x65, 0x65,
    0x9B, 0x9D, 0x89, 0xB9, 0xA9, 0xDF, 0xCC, 0xF4,
    0x12, 0x05, 0x2E, 0x53, 0x45, 0x68, 0x65, 0x65,
    0x9B, 0x9C, 0x8B, 0xBA, 0xB5, 0xD8, 0xCB, 0xFF,
    0xE4, 0x02, 0x37, 0x58, 0x43, 0x6B, 0x65, 0x65,
    0x9B, 0x9F, 0x8A, 0x85, 0xB6, 0xA1, 0xD6, 0xC4,
    0xEF, 0x14, 0x32, 0x5A, 0x4D, 0x75, 0x65, 0x65,
    0x9B, 0x9E, 0x94, 0x81, 0xBD, 0xAF, 0xDC, 0xC2,
    0xF6, 0x18, 0x04, 0x22, 0x4E, 0x76, 0x65, 0x65,
    0x9A, 0x99, 0x96, 0x8C, 0xB8, 0xB5, 0xA5, 0xD5,
    0xFB, 0xEE, 0x15, 0x34, 0x52, 0x7E, 0x67, 0x65,
    0x9A, 0x9B, 0x93, 0x8A, 0x80, 0xBF, 0xA9, 0xDB,
    0xCA, 0xF8, 0xEF, 0x08, 0x24, 0x42, 0x6F, 0x64,
    0x9A, 0x9A, 0x9F, 0x93, 0x8A, 0x83, 0xB9, 0xB4,
    0xA0, 0xD3, 0xC3, 0xF7, 0x03, 0x54, 0x72, 0x60,
    0x9A, 0x9A, 0x9B, 0x9E, 0x9D, 0x97, 0x8E, 0x81,
    0xBF, 0xA9, 0xD9, 0xCF, 0xF7, 0x3E, 0x44, 0x62,
};

#ifdef _MULTI_OD_
BYTE code tMultiODTbl8_1[] =
{
    0xC8, 0xD9, 0xED, 0xF2, 0x9B, 0xA4, 0x4F, 0x56,
    0x7D, 0xC8, 0x12, 0x23, 0x30, 0x35, 0x36, 0x37,
    0xC8, 0xD8, 0xEA, 0xFE, 0x86, 0xAE, 0x49, 0x52,
    0x79, 0x0E, 0x12, 0x23, 0x30, 0x35, 0x36, 0x37,
    0xC8, 0xC6, 0xE8, 0xFB, 0x81, 0x97, 0xB1, 0x58,
    0x60, 0x74, 0x18, 0x2B, 0x3C, 0x35, 0x36, 0x37,
    0xC8, 0xC5, 0xD6, 0xF8, 0x8C, 0x91, 0xBA, 0x42,
    0x68, 0x70, 0x03, 0x17, 0x39, 0x34, 0x36, 0x37,
    0xC8, 0xC4, 0xD4, 0xE5, 0x88, 0x9C, 0xA3, 0x4B,
    0x52, 0x79, 0x0D, 0x12, 0x26, 0x33, 0x36, 0x37,
    0xC8, 0xC3, 0xD2, 0xE3, 0xF5, 0x98, 0xAD, 0xB4,
    0x5B, 0x63, 0x09, 0x1D, 0x23, 0x32, 0x35, 0x37,
    0xC8, 0xC2, 0xD0, 0xE0, 0xF2, 0x84, 0xA8, 0xBD,
    0x44, 0x6C, 0x73, 0x18, 0x2F, 0x31, 0x35, 0x37,
    0xC8, 0xC0, 0xDF, 0xEE, 0xFF, 0x80, 0x93, 0xB8,
    0x4E, 0x55, 0x7E, 0x03, 0x2A, 0x3F, 0x35, 0x37,
    0xC8, 0xCF, 0xDD, 0xEC, 0xFB, 0x8B, 0x9E, 0xA2,
    0x48, 0x5F, 0x67, 0x0E, 0x16, 0x3D, 0x35, 0x36,
    0xC8, 0xCD, 0xDC, 0xE9, 0xF8, 0xF7, 0x98, 0xAC,
    0xB1, 0x58, 0x60, 0x08, 0x10, 0x39, 0x34, 0x36,
    0xC8, 0xCA, 0xDA, 0xD6, 0xE4, 0xF2, 0x82, 0x94,
    0xB9, 0x40, 0x68, 0x71, 0x18, 0x26, 0x34, 0x36,
    0xC8, 0xC8, 0xD8, 0xD3, 0xE0, 0xFD, 0x8B, 0x9B,
    0xAF, 0xB6, 0x5E, 0x78, 0x01, 0x20, 0x32, 0x36,
    0xC8, 0xC8, 0xC5, 0xD0, 0xEB, 0xE7, 0xF4, 0x82,
    0x93, 0xB8, 0x42, 0x6C, 0x08, 0x16, 0x3F, 0x36,
    0xC8, 0xC8, 0xC2, 0xDB, 0xD5, 0xEF, 0xF8, 0xF5,
    0x82, 0x96, 0xBB, 0x46, 0x78, 0x18, 0x25, 0x35,
    0xC8, 0xC8, 0xC0, 0xC7, 0xDE, 0xEA, 0xE2, 0xF8,
    0xF2, 0x83, 0x90, 0xBC, 0x5B, 0x08, 0x28, 0x34,
    0xC8, 0xC8, 0xCF, 0xC4, 0xD9, 0xD0, 0xD4, 0xEB,
    0xF9, 0xF2, 0x8C, 0x9A, 0xAF, 0x5F, 0x77, 0x38,
    0xC8, 0xC8, 0xCF, 0xC4, 0xD9, 0xD0, 0xD4, 0xEB,
    0xE0, 0xE7, 0xFF, 0x89, 0x98, 0xA0, 0x4D, 0x7D,
    0x49, 0x5A, 0x6E, 0x07, 0x1E, 0x39, 0xD2, 0xE8,
    0xFE, 0x91, 0xA2, 0xB2, 0xBA, 0xBF, 0xBE, 0xBE,
    0x46, 0x58, 0x6D, 0x41, 0x1B, 0x32, 0xCE, 0xE4,
    0xFD, 0x91, 0xA2, 0xB3, 0xBA, 0xBF, 0xBE, 0xBE,
    0x47, 0x59, 0x68, 0x7C, 0x15, 0x2D, 0xC4, 0xDD,
    0xF2, 0x84, 0x9D, 0xAD, 0xBB, 0xBF, 0xBE, 0xBE,
    0x45, 0x54, 0x66, 0x7B, 0x0F, 0x24, 0x3F, 0xD7,
    0xED, 0x80, 0x93, 0xA9, 0xB9, 0xBC, 0xBE, 0xBE,
    0x40, 0x55, 0x65, 0x76, 0x0B, 0x21, 0x36, 0xCF,
    0xE5, 0xFA, 0x8E, 0xA5, 0xB7, 0xBC, 0xBF, 0xBE,
    0x41, 0x52, 0x63, 0x75, 0x06, 0x1A, 0x31, 0xC6,
    0xDC, 0xF7, 0x8A, 0xA1, 0xB5, 0xBD, 0xBF, 0xBE,
    0x41, 0x50, 0x61, 0x70, 0x03, 0x16, 0x2A, 0xC0,
    0xD9, 0xEE, 0x84, 0x9D, 0xB0, 0xBD, 0xBF, 0xBE,
    0x41, 0x51, 0x5F, 0x6F, 0x7E, 0x10, 0x27, 0x3A,
    0xD0, 0xE8, 0x80, 0x97, 0xAE, 0xBA, 0xBF, 0xBE,
    0x41, 0x4E, 0x5D, 0x6A, 0x7A, 0x0D, 0x21, 0x34,
    0xCA, 0xE2, 0xFA, 0x90, 0xAA, 0xBB, 0xBF, 0xBE,
    0x41, 0x4C, 0x5B, 0x69, 0x76, 0x06, 0x1B, 0x2F,
    0xC4, 0xDD, 0xF5, 0x8A, 0xA4, 0xB8, 0xBF, 0xBE,
    0x41, 0x4D, 0x59, 0x64, 0x72, 0x03, 0x12, 0x27,
    0x3D, 0xD4, 0xED, 0x84, 0xA0, 0xB7, 0xBC, 0xBE,
    0x41, 0x4B, 0x54, 0x60, 0x6E, 0x7D, 0x0A, 0x1C,
    0x33, 0xCB, 0xE5, 0xFD, 0x98, 0xB2, 0xBC, 0xBE,
    0x41, 0x49, 0x53, 0x5C, 0x69, 0x74, 0x03, 0x13,
    0x24, 0x3A, 0xD4, 0xF2, 0x8C, 0xAC, 0xBD, 0xBE,
    0x41, 0x44, 0x4E, 0x56, 0x60, 0x6D, 0x76, 0x05,
    0x12, 0x26, 0x3E, 0xDF, 0x80, 0x9E, 0xB8, 0xBE,
    0x41, 0x42, 0x4D, 0x50, 0x5B, 0x64, 0x6D, 0x76,
    0x7E, 0x13, 0x26, 0xC5, 0xEB, 0x8C, 0xB1, 0xBF,
    0x41, 0x41, 0x4B, 0x4E, 0x54, 0x5B, 0x61, 0x64,
    0x74, 0x7E, 0x0B, 0x1D, 0x39, 0xF0, 0x96, 0xBC,
    0x41, 0x41, 0x4B, 0x4E, 0x54, 0x5B, 0x61, 0x64,
    0x6D, 0x72, 0x7D, 0x06, 0x18, 0x35, 0xDB, 0x9F,
    0xFD, 0xEC, 0xD9, 0xC4, 0xAF, 0x91, 0x7A, 0x63,
    0x48, 0x35, 0x27, 0x16, 0x05, 0x00, 0x03, 0x02,
    0xFD, 0xF2, 0xDC, 0xC9, 0xB6, 0x9E, 0x80, 0x69,
    0x56, 0x3D, 0x29, 0x1A, 0x0B, 0xFD, 0x03, 0x02,
    0xFD, 0xF3, 0xE2, 0xCF, 0xBB, 0xA1, 0x88, 0x70,
    0x59, 0x46, 0x30, 0x1F, 0x0E, 0x01, 0x03, 0x02,
    0xFD, 0xF0, 0xE0, 0xD2, 0xBF, 0xAA, 0x92, 0x7B,
    0x60, 0x49, 0x34, 0x20, 0x0D, 0x01, 0x03, 0x02,
    0xFD, 0xF1, 0xE6, 0xD1, 0xC2, 0xAF, 0x95, 0x7D,
    0x6B, 0x50, 0x3E, 0x2A, 0x10, 0x06, 0x03, 0x02,
    0xFD, 0xF6, 0xE4, 0xD7, 0xC6, 0xB2, 0x9E, 0x84,
    0x6D, 0x5A, 0x40, 0x2E, 0x15, 0x04, 0x00, 0x02,
    0xFD, 0xF4, 0xE5, 0xDA, 0xC5, 0xB7, 0xA3, 0x8E,
    0x74, 0x5C, 0x45, 0x30, 0x19, 0x05, 0x00, 0x02,
    0xFD, 0xF5, 0xEA, 0xD8, 0xC8, 0xBB, 0xA5, 0x93,
    0x7E, 0x66, 0x4E, 0x35, 0x1D, 0x08, 0x00, 0x02,
    0xFD, 0xFB, 0xE9, 0xDF, 0xCF, 0xBC, 0xAE, 0x95,
    0x83, 0x69, 0x51, 0x3E, 0x26, 0x09, 0x00, 0x03,
    0xFD, 0xF9, 0xEE, 0xDD, 0xD2, 0xC1, 0xB3, 0x9D,
    0x88, 0x70, 0x59, 0x41, 0x28, 0x0D, 0x01, 0x03,
    0xFD, 0xFD, 0xED, 0xE0, 0xD7, 0xCA, 0xBA, 0xA5,
    0x91, 0x7E, 0x60, 0x49, 0x33, 0x11, 0x06, 0x03,
    0xFD, 0xFD, 0xF2, 0xE7, 0xDB, 0xCF, 0xC2, 0xB2,
    0x9C, 0x85, 0x6C, 0x51, 0x38, 0x19, 0x05, 0x03,
    0xFD, 0xFD, 0xF1, 0xEB, 0xDD, 0xD6, 0xCA, 0xBE,
    0xAE, 0x95, 0x83, 0x67, 0x47, 0x25, 0x0E, 0x03,
    0xFD, 0xFD, 0xF5, 0xEF, 0xE4, 0xD9, 0xD1, 0xC4,
    0xB9, 0xAF, 0x97, 0x7F, 0x5F, 0x34, 0x15, 0x01,
    0xFD, 0xFD, 0xFA, 0xF1, 0xEB, 0xE1, 0xDE, 0xD4,
    0xCC, 0xC7, 0xAE, 0x99, 0x7E, 0x50, 0x28, 0x05,
    0xFD, 0xFD, 0xFA, 0xF1, 0xEC, 0xE5, 0xE1, 0xDE,
    0xD5, 0xD2, 0xCA, 0xBC, 0xAD, 0x94, 0x5B, 0x25,
    0x90, 0x82, 0xB6, 0xDD, 0xC7, 0xE0, 0x0B, 0x31,
    0x26, 0x48, 0x7B, 0x6A, 0x63, 0x66, 0x67, 0x67,
    0x9E, 0x80, 0xB2, 0xA7, 0xCE, 0xF7, 0x11, 0x38,
    0x2F, 0x52, 0x46, 0x74, 0x63, 0x66, 0x67, 0x67,
    0x9D, 0x8E, 0xB0, 0xA3, 0xC9, 0xF1, 0x19, 0x98,
    0x37, 0x5D, 0x4F, 0x73, 0x62, 0x65, 0x67, 0x67,
    0x9A, 0x8D, 0xBE, 0xA0, 0xD4, 0xFA, 0xE2, 0x09,
    0x30, 0x26, 0x4A, 0x7D, 0x6F, 0x65, 0x67, 0x67,
    0x98, 0x8B, 0xBB, 0xAD, 0xD0, 0xC5, 0xEB, 0x13,
    0x3A, 0x21, 0x54, 0x7A, 0x6C, 0x65, 0x66, 0x67,
    0x98, 0x8A, 0xB9, 0xAA, 0xDC, 0xC0, 0xF5, 0x1C,
    0x03, 0x2A, 0x50, 0x46, 0x6B, 0x64, 0x66, 0x67,
    0x98, 0x88, 0x87, 0xA8, 0xD8, 0xCC, 0xF0, 0xE6,
    0x0D, 0x35, 0x5B, 0x41, 0x68, 0x64, 0x66, 0x67,
    0x98, 0x97, 0x85, 0xB5, 0xA5, 0xD7, 0xFB, 0xE0,
    0x16, 0x3E, 0x26, 0x4B, 0x75, 0x63, 0x66, 0x67,
    0x98, 0x96, 0x83, 0xB2, 0xA1, 0xD2, 0xC4, 0xEA,
    0x10, 0x07, 0x20, 0x56, 0x71, 0x61, 0x66, 0x67,
    0x98, 0x94, 0x81, 0xBF, 0xAD, 0xDD, 0xCE, 0xF2,
    0x19, 0x00, 0x29, 0x51, 0x7C, 0x60, 0x65, 0x67,
    0x98, 0x93, 0x8F, 0xBB, 0xA9, 0xA7, 0xD7, 0xF9,
    0xEF, 0x17, 0x30, 0x59, 0x45, 0x6D, 0x65, 0x67,
    0x98, 0x91, 0x8C, 0xB8, 0xB4, 0xA0, 0xDF, 0xC0,
    0xF4, 0x1B, 0x05, 0x20, 0x4B, 0x69, 0x65, 0x67,
    0x98, 0x9E, 0x88, 0x83, 0xBE, 0xA8, 0xA5, 0xD3,
    0xC4, 0xEA, 0x14, 0x32, 0x50, 0x7D, 0x63, 0x67,
    0x98, 0x9B, 0x95, 0x8D, 0x85, 0xB0, 0xA9, 0xA5,
    0xD4, 0xC4, 0xED, 0x09, 0x21, 0x40, 0x6E, 0x66,
    0x98, 0x98, 0x92, 0x97, 0x80, 0x87, 0xBE, 0xB5,
    0xAD, 0xD4, 0xC2, 0xF6, 0x00, 0x59, 0x70, 0x66,
    0x98, 0x98, 0x92, 0x97, 0x8D, 0x82, 0xB8, 0xBD,
    0xB4, 0xAB, 0xA4, 0xD0, 0xC2, 0x0F, 0x23, 0x60,
};

code BYTE *tOverDrive_AllTbl[] =
{
   tOverDrive_120Y1, tOverDrive_120Y2, tOverDrive_120Y3, tOverDrive_120Y4,
   tOverDrive_120Y5, tOverDrive_120Y6, tOverDrive_120Y7, tOverDrive_120Y8,
   tOverDrive_110, tOverDrive_110, tOverDrive_110, tOverDrive_110,
   tOverDrive_110, tOverDrive_110, tOverDrive_110, tOverDrive_110,
   tOverDrive_100, tOverDrive_100, tOverDrive_100, tOverDrive_100,
   tOverDrive_100, tOverDrive_100, tOverDrive_100, tOverDrive_100
};

void msLoad_MultiODTable( BYTE index )
{
    idata WORD wCount,wXORKeyIdx;
    idata BYTE ucXORKey,OD_data,ucMultiTblIdx,ucSramIdx,ucTblData;
    idata DWORD dwXdataAddr;
    idata BYTE ucReg;
    idata BYTE ucOffset;

#if 0
    if(0 == tOverDrive_AllTbl) return;
#endif

    mcuXdataMapToDRAM(MIU_OD_MTB_ADDR);
    msWriteByte(REG_3100, 0x00);

#if (GLASSES_TYPE==GLASSES_NVIDIA)
    msWriteByte(REG_3123, 0x5F);
    msWriteByte(REG_3124, 0x0C);
    msWriteByte(REG_3175, 0x3F);
#endif

    ucReg = msReadByte(REG_3120);
    msWriteBit(REG_3120, 0, _BIT0); // msWriteByte(REG_3120, 0x00); // Disable OD before load code
    while(msReadByte(REG_3120)&_BIT0);
    msWriteByte(REG_3102, 0x00);    // load code start

    dwXdataAddr = (WIN0_ADDR_START<<10);

    wCount=0;

    ucOffset = index*8;

    for(ucMultiTblIdx =0;ucMultiTblIdx < 8; ucMultiTblIdx++)
    {
        while(wCount<288)
        {
            for(ucSramIdx = 0;ucSramIdx < 4;ucSramIdx++)
            {
                if(ucSramIdx<3)
                    wXORKeyIdx =(9 + 10*ucSramIdx) + 272*(WORD)(ucSramIdx);
                else
                    wXORKeyIdx =(9 + 10*ucSramIdx) + 256*(WORD)(ucSramIdx-2) + 272*2;

                if( wCount == 9 && ucSramIdx == 0)
                    ucXORKey = 0;
                else if( wCount == 19 && ucSramIdx == 1)
                    ucXORKey = 0;
                else if( wCount == 29 && ucSramIdx == 2)
                    ucXORKey = 0;
                else if( wCount == 39 && ucSramIdx == 3)
                    ucXORKey = 0;
                else
                    ucXORKey =*(tOverDrive_AllTbl[ucMultiTblIdx+ucOffset]+wXORKeyIdx);

                if(ucSramIdx < 2 && wCount < 272)
                {
                    ucTblData = *(tOverDrive_AllTbl[ucMultiTblIdx+ucOffset]+(272*(WORD)ucSramIdx) + wCount ) ;
                    OD_data = ucXORKey ^ ucTblData;
                    msRegs[dwXdataAddr + ucSramIdx] = OD_data;
                }
                else if(ucSramIdx >=2 && wCount < 256)
                {
                    ucTblData = *(tOverDrive_AllTbl[ucMultiTblIdx+ucOffset]+(256*(WORD)(ucSramIdx-2) + 272*2) + wCount ) ;
                    OD_data = ucXORKey ^ ucTblData;
                    msRegs[dwXdataAddr + ucSramIdx] = OD_data;
                }
            }
            wCount++;
            dwXdataAddr += 0x10; //16 bytes alligment
        }
        wCount = 0;
    }

    msWriteBit(REG_31E1, _ENABLE, _BIT7);
    msWriteByteMask(REG_31E1, _BIT2 ,_BIT2 |_BIT1);

    msWriteByte(REG_31E3, 0x07);    // 8 multi-table

    msWriteByte(REG_3100, 0x01);
    msWriteBit(REG_3102, _ENABLE, _BIT1);
    msWrite2Byte(REG_3112, 288);     // sram size 288
    msWriteByte(REG_3118, 0x20);
    msWrite2Byte(REG_3108, (MIU_OD_MTB_ADDR+(WIN0_ADDR_START<<10))>>4);
    msWrite2Byte(REG_310A, (MIU_OD_MTB_ADDR+(WIN0_ADDR_START<<10))>>20);

    msWriteByte(REG_3100, 0x00);
    msWriteBit(REG_31E4, _ENABLE, _BIT0);
    msWriteByte(REG_31E0, 91);    //#line start
    msWriteByte(REG_3120, ucReg);

    mcuXdataMapToDRAM(HK_XDATA_ADDR);
}
#endif

void msOverDriveOnOff( BOOL bEnable )
{
   drvmsOverDriveOnOff(bEnable);
}

void msOverDriverInit( void )
{
    drvmsOverDriverInit();
}
void msRTE_LoadLUT(void)
{
    drvmsLoadODTable(tOverDrive);
}

#if (GLASSES_TYPE!=GLASSES_NONE)
void msODWatchTrackPointValue(BOOL bAfterOD)
{
    WORD uwTemp1,uwTemp2;
    msWriteByte(REG_3100, 0x00);

    if(bAfterOD)
    {
        msWriteByteMask(REG_317A, _BIT1, _BIT2|_BIT1);
    }
    else
    {
        msWriteByteMask(REG_317A, _BIT2|_BIT1, _BIT2|_BIT1);
    }

    uwTemp1 = msRead2Byte(REG_318A);
    uwTemp2 = msRead2Byte(REG_318C);

    //printf("\r\n OD_R(%d)", (uwTemp1&0x3FF)>>2);
    //printf(" OD_G(%d)", (((uwTemp1&0xFC00)>>10)|(uwTemp2&0x000F)<<6)>>2);
    //printf(" OD_B(%d)\r\n", (uwTemp2&0x3FF0)>>6);

}
#endif

#else
BYTE code msOVDDummy[] = {0};
void msOverDriveDummy(void)
{
    BYTE x = msOVDDummy;
}

#endif
